<?php
class DefaultController extends Controller {
    
    public function actionIndex() {
        if( Yii::app()->request->isPostRequest ) {
            $upload = CUploadedFile::getInstanceByName("push");
            $storage = new Storage();
            $result = $storage->writeData( new ImageData( $upload->tempName ) );
            if( isset($result['_id']) ) {
                $storage->thumb( $result['_id'] );
                $link = $this->createUrl("default/images",array('id'=>$result['_id']));
                echo "访问你的图片：",CHtml::link( $link, $link, array("target"=>"_blank") );
            }
        }
        
        $this->render("index");
    }
    
    public function actionImages($id) {
        $id = Yii::app()->request->getQuery('id');
        $storage = new Storage();
        $data = $storage->readData( $id );
        if( !is_null( $data ) ) {
            header( "Content-Type:".$data['mimeType'] );
            echo $data['data']->bin;
        }
        else {
            throw new CHttpException( 404 );
        }
    }
}